package com.example.yzj16478026;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.yzj16478026.utils.PropertiesUtils;

public class activity_register extends Activity {
    private EditText regusn,regpwd;
    private Button regbtn;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //初始化事件绑定
        init();
        //注册事件监听
        regEvent();
    }

    private void regEvent() {
        regusn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                String usn=((EditText)view).getText().toString();
                if (PropertiesUtils.getPwdForRegName(usn)==null){
                    EditText t=(EditText)view;
                    t.setBackgroundResource(R.drawable.input_right);
                }
                if (PropertiesUtils.getPwdForRegName(usn)!=null){
                    EditText t=(EditText)view;
                    t.setBackgroundResource(R.drawable.input_error);
                }
                if (usn.equals("")){
                    EditText t=(EditText)view;
                    t.setBackgroundResource(R.drawable.input);
                }
            }
        });
        regbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usn=regusn.getText().toString();
                String pwd=regpwd.getText().toString();
                if (PropertiesUtils.getPwdForRegName(usn)!=null){
                    Toast.makeText(activity_register.this,"用户名已存在，注册失败!",Toast.LENGTH_SHORT).show();
                }else {
                    PropertiesUtils.putUser(usn,pwd);
                    Intent intent=new Intent(activity_register.this,LoginActivity.class);
                    intent.putExtra("msg","用户"+usn+"注册成功!请登录!");
                    startActivity(intent);
                }
            }
        });
    }

    private void init() {
        regusn=findViewById(R.id.regusn);
        regpwd=findViewById(R.id.regpwd);
        regbtn=findViewById(R.id.reg_btn);
    }
}
